/*
 * Home.java
 *
 * Created on January 29, 2007, 11:11 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package ge.info.pages;

import ge.info.domain.AbonentFound;
import ge.info.domain.Country;
import ge.info.domain.Service;
import ge.ktapestry.pages.BasePageExt;
import ge.ktools.StringUtils;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.InitialValue;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;

import java.util.ArrayList;
import java.util.List;
import org.apache.hivemind.util.PropertyUtils;
import org.apache.tapestry.IPage;
import org.apache.tapestry.annotations.InjectState;

public abstract class SearchInd extends BasePageExt implements PageBeginRenderListener
{

    @InjectState("service")
    public abstract Service getService();

    public abstract void setService(Service service);

    @Persist
    public abstract Country getCountry();

    public abstract void setCountry(Country country);

    @Persist
    public abstract AbonentFound getAbonentFound();

    public abstract void setAbonentFound(AbonentFound abonentFound);

    @Persist
    public abstract List<AbonentFound> getAbonentsFound();

    public abstract void setAbonentsFound(List<AbonentFound> abonentsFound);


    public abstract AbonentFound getLoopAbonentFound();

    public abstract void setLoopAbonentFound(AbonentFound abonentFound);

    @Persist
    public abstract int getServiceID();

    public abstract void setServiceID(int serviceID);

    @InitialValue("literal:1")
    @Persist("client")
    public abstract int getSearchType();

    public abstract void setSearchType(int searchType);

    public void pageBeginRender(PageEvent event)
    {
        if (getSearchType() == 1)
        {
            setServiceID(7);

            //getAbonentFound().getPhone().setPhone(null);
        }
        else
        {
            setServiceID(46);
         
        }

        if (!event.getRequestCycle().isRewinding())
        {
            
            
            if (getAbonentFound() == null)
            {
                
                setAbonentFound(new AbonentFound());
                
            }
        } else
        {
            setAbonentsFound(new ArrayList<AbonentFound>());
        }
       
    }

//    public void searchAbroad(IRequestCycle cycle)
//    {
//        AbonentFound af = getAbonentFound();
//        if (af.getFirstName() != null)
//        {
//            af.setFirstName(af.getFirstName().trim());
//            if (af.getFirstName().length() < 2)
//            {
//                af.setFirstName(null);
//            }
//        }
//        if (af.getLastName() != null)
//        {
//            af.setLastName(af.getLastName().trim());
//            if (af.getLastName().length() < 2)
//            {
//                af.setLastName(null);
//            }
//        }
//        if (af.getFirstName() == null && af.getLastName() == null)
//        {
//            getDelegate().setFormComponent((IFormComponent) this.getComponent("lastNameAbroad"));
//            ValidatorException ex = new ValidatorException("lastNameAbroad");
//            getDelegate().record(ex);
//            return;
//        }
//        setAbonentsFound(getDbExecutor().searchInd(af, getCountry()));
//    }
    public void search(IRequestCycle cycle)
    {
        AbonentFound af = getAbonentFound();
        if (af.getFirstName() != null)
        {
            af.setFirstName(af.getFirstName().trim());
        } else
        {
            af.setFirstName("");
        }
        if (af.getLastName() != null)
        {
            af.setLastName(af.getLastName().trim());
        } else
        {
            af.setLastName("");
        }

        if (af.getAddress().getCityRegion().getCityID() == null || af.getAddress().getCityRegion().getCityID() == 0)
        {
            af.getAddress().getCityRegion().setCityID(null);
        }

        setAbonentsFound(getDbExecutor().searchInd(af, getSession()));


    }

    public void clear(IRequestCycle cycle)
    {
        setAbonentFound(new AbonentFound());
    }

    public void searchPhone(IRequestCycle cycle)
    {
        setAbonentsFound(getDbExecutor().searchIndByPhone(getAbonentFound().getPhone().getPhone().trim(), getSession()));
        getAbonentsFound().addAll(getDbExecutor().searchOrgByPhone(getAbonentFound().getPhone().getPhone().trim()));
    }

    public void openOrgResultPage(IRequestCycle cycle, AbonentFound abonentFound)
    {
        Integer id = abonentFound.getMainID();
        setAbonentFound(new AbonentFound());
        IPage page = cycle.getPage("OrgResultPage");
        PropertyUtils.write(page, "organization", null);
        PropertyUtils.write(page, "organizationID", id);
        cycle.activate(page);
    }

    public void sendSMS(IRequestCycle cycle, AbonentFound abonent)
    {


        StringBuilder sb = new StringBuilder();

        sb.append(abonent.getFirstName()).append(" ");
        sb.append(abonent.getLastName()).append(" ");

        if (abonent.getAddress().getStreetName() != null)
        {
            sb.append(abonent.getAddress().getStreetName()).append(" ");
        }

        if (abonent.getPhone() != null || abonent.getPhone().getHide() == 0)
        {
            sb.append(StringUtils.toNumbers(abonent.getPhone().getPhone())).append(" ");
        }


        getDbExecutor().insertSms(getSession(), sb.toString(), getServiceID());
    }
}
